package com.lemon.common.web.advice;

import com.lemon.common.exception.ExceptionEnum;
import com.lemon.common.exception.BaseException;
import com.lemon.common.dto.Response;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 异常处理
 *
 * @author ymh
 * @date 2019-3-22 17:52
 */
@ControllerAdvice
public class CommonExceptionHandler {

    /**
     * 捕获 BaseException 异常，并处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BaseException.class)
    public ResponseEntity<Response> handleException(BaseException e) {
        ExceptionEnum em = e.getExceptionEnum();
        return ResponseEntity.status(em.getCode()).body(new Response(em));
    }
}
